package com.p2p.front.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.p2p.base.exception.BizException;
import com.p2p.base.model.BaseUser;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.dal.model.Bank;
import com.p2p.front.dal.model.BankCard;
import com.p2p.front.dal.model.BankInfo;
import com.p2p.front.service.BankCardService;
import com.p2p.front.service.BankInfoService;
import com.p2p.front.service.BankService;
import com.p2p.front.service.UserInfoAuthService;
import com.p2p.user.enums.UserAuthName;
import com.p2p.user.facade.UserQueryFacade;
import com.p2p.user.result.UserQueryResult;
import com.p2p.user.vo.UserInfoFullVO;

/**
 * @author ruiqi
 *
 */
@RequestMapping("/bankCard")
@Controller
public class BankCardController extends BaseController {

	@Resource
	private BankCardService bankCardService;

	@Resource
	private UserQueryFacade remoteUserQueryFacade;

	@Resource
	private BankService bankService;
	
	@Resource
	private UserInfoAuthService userInfoAuthService;
	
	@Resource
	private BankInfoService bankInfoService;

	@RequestMapping("/index")
	public String cardIndex(ModelMap modelMap) {
		BaseUser baseUser = getCurrentUser();
		
		
		List<BankCard> cardList = bankCardService.listAllCardByUser(baseUser
				.getId());
		List<Bank> bankList = bankService.listAll();
		modelMap.put("cardList", cardList);
		modelMap.put("bankList", bankList);
		
		List<UserAuthName> authNameList = new ArrayList<UserAuthName>();
		
		authNameList.add(UserAuthName.AUTH_REALNAME);
		
		Map<Integer, Character> resultMap = userInfoAuthService
				.getAuthByEnumList(baseUser
						.getId(), authNameList);
		modelMap.addAttribute("realNameStatus",
				resultMap.get(UserAuthName.AUTH_REALNAME.getIndex()).toString());
		return "/bank/index";
	}

	@RequestMapping("/saveBankCard")
	public String addCard(BankCard bankCard, ModelMap model) {
		BaseUser baseUser = getCurrentUser();
		bankCard.setUid(baseUser.getId());
		/*
		String bank =bankCard.getBank();
		
		String[] codeAndBank=bank.split("-");
	
		bankCard.setCode(codeAndBank[0]);
		bankCard.setBank(codeAndBank[1]);*/

		bankCard.setBranch(bankCard.getBranch());
		UserQueryResult userQueryResult = remoteUserQueryFacade
				.getUserInfoById(bankCard.getUid());
		UserInfoFullVO userInfoFullVO = userQueryResult.getUserInfoFullVO();

		String realName = userInfoFullVO.getRealname();
		if (StringUtils.isBlank(realName)) {
			throw new BizException(null,"真实姓名未登记，请先实名认证");
		}
		Character autoRealNameValue = userInfoFullVO.getAuthMap().get(UserAuthName.AUTH_REALNAME.getIndex());
		if (autoRealNameValue==null||autoRealNameValue=='0') {
			throw new BizException(null,"真实姓名未认证，请先实名认证");
		}
		bankCard.setName(realName);
		bankCardService.saveNewCard(bankCard);
		model.clear();

		return "redirect:/bankCard/index.html";
	}

	@RequestMapping("/editCard")
	public String editCard(BankCard bankCard, ModelMap modelMap) {

		BaseUser baseUser = getCurrentUser();
		bankCard.setUid(baseUser.getId());

		UserQueryResult userQueryResult = remoteUserQueryFacade
				.getUserInfoById(bankCard.getUid());
		UserInfoFullVO userInfoFullVO = userQueryResult.getUserInfoFullVO();

		String realName = userInfoFullVO.getRealname();

		if (StringUtils.isBlank(realName)) {
			throw new BizException(null,"真实姓名未登记，实名认证通过之后才能绑定银行卡");
		}
		bankCardService.updateCardInfo(bankCard);

		List<BankCard> cardList = bankCardService.listAllCardByUser(baseUser
				.getId());
		modelMap.put("cardList", cardList);
		return "/bank/index";
	}

	@RequestMapping("/removeCard")
	public void removeCard(BankCard bankCard, ModelMap model) {
		BaseUser baseUser = getCurrentUser();
		bankCard.setUid(baseUser.getId());
		bankCardService.removeCard(bankCard);
		model.addAttribute("code", 1);
	}

	@RequestMapping("/getBankInfo")
	public void getBankInfo( String code,Integer area,Integer city,HttpServletResponse response){
		List<BankInfo> bankInfo = this.bankInfoService.getBankInfo(code, area, city);
		this.returnJsonObject(response, bankInfo);
		return ;
	}
}
